package com.linkcard.list.action;

import java.io.PrintWriter;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.linkcard.commen.tools.Helper;
import com.linkcard.commen.tools.Page;
import com.linkcard.list.manager.LinksManager;
import com.linkcard.list.model.Links;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LinksAction extends ActionSupport {

	private LinksManager linksManager=null;
	private int page;
	private int rows;
	private Links links=null;

	@Override
	public String execute() throws Exception {
		return "success";
	}

	//添加友情链接
	public String addLinks() throws Exception{
		
		links.setName(Helper.decode(links.getName()));
		links.setIntroduction(Helper.decode(links.getIntroduction()));
		boolean isSuccess=linksManager.addLinks(links);
		if(isSuccess)
		{
			settingWebsiteInfo();
		}
		JSONObject josn=new JSONObject();
		josn.put("info",isSuccess);
		backText(josn.toString());
		return "Data";
	}
	//获得链接列表　
	public String linksListForJson() throws Exception{
		Page pageObj=new Page(); 
		pageObj.setCurrentPage(page);
		pageObj.setRowCount(rows);
		List list=this.linksManager.linksList(pageObj);
		JSONObject json=new JSONObject();
		json.put("total", pageObj.getRowCount());
		JSONArray jsonArray=new JSONArray();
		for(int i=0;i<list.size();i++)
		{
			Links links=(Links)list.get(i);
			JSONObject tempObject=new JSONObject();
			tempObject.put("id", links.getId());
			tempObject.put("url", links.getUrl());
			tempObject.put("name", links.getName());
			tempObject.put("image", "<a target='_blank' href='"+links.getUrl()+"'><img style='border:0px;' width='160' height='60' src='" +links.getImage()+"'/></a>");
			tempObject.put("introduction", links.getIntroduction());
			tempObject.put("sort", links.getSort());
			tempObject.put("recommend", (links.getRecommend()==0)?"否":"是");
			tempObject.put("opt", "<a href='javascript:void(0);' onclick='modifyLinks("+links.getId()+")'>修改</a>&nbsp;&nbsp;<a href='javascript:void(0);' onclick='deleteLinks("+links.getId()+")'>删除</a>");
			jsonArray.add(tempObject);
		}
		json.put("rows", jsonArray);
		backText(json.toString());
		return "Data";
	}
	//获得单个链接
	public String singleLinks() throws Exception{
		
		Links links=this.linksManager.singleLinks(this.links.getId());
		JSONObject json=JSONObject.fromObject(links);
		backText(json.toString());
		return "Data";
	}
	//修改链接
	public String modifyLinks() throws Exception{
		links.setName(Helper.decode(links.getName()));
		links.setIntroduction(Helper.decode(links.getIntroduction()));
		boolean isSuccess=this.linksManager.modifyLinks(links);
		if(isSuccess)
		{
			settingWebsiteInfo();
		}
		JSONObject josn=new JSONObject();
		josn.put("info",isSuccess);
		backText(josn.toString());
		return "Data";
	}
	//删除链接
	public String deleteLinks() throws Exception{
		
		boolean isSuccess=this.linksManager.deleteLinks(links.getId());
		JSONObject josn=new JSONObject();
		josn.put("info",isSuccess);
		backText(josn.toString());
		return "Data";
	}
	//返回文本
	public void backText(String data) throws Exception
	{
		ServletActionContext.getResponse().setContentType ("text/html;charset=utf-8");
		
		  PrintWriter out = ServletActionContext.getResponse().getWriter();
		  out.print(data);
		  out.flush();
		  out.close();
	}
	/**
	 * 将网站修改的内容设置到application
	 * @return
	 */
	public void settingWebsiteInfo()
	{
		List links=linksManager.linksListForRecommend();
		ActionContext.getContext().getApplication().put("links",links);
	}
	public LinksManager getLinksManager() {
		return linksManager;
	}


	public void setLinksManager(LinksManager linksManager) {
		this.linksManager = linksManager;
	}


	public Links getLinks() {
		return links;
	}


	public void setLinks(Links links) {
		this.links = links;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}
	
}
